package Utils;

import Beans.Inventory;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPath;
import io.restassured.response.Response;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import static io.restassured.RestAssured.given;

public class Utils extends BasicTest{
    private static Response response;
    static JSONObject json;

    //获取今日日期
    public static String getCurrentDate(){
        String currentDate;
        Date dt=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        currentDate=sdf.format(dt);
        return  currentDate;
    }

    //查询库存
    public static Inventory queryInventory( String sitecode, String skucode){
        Inventory inventory=new Inventory();
         response=given()
                .contentType("application/json")
                .headers("x-yh-wms-whid","170952316587872256")
                .cookies(cookies)
                .body("{\"pagesize\":50,\"pageindex\":1,\"sitecode\":\""+sitecode+"\",\"skucode\":\""+skucode+"\"}")
                .post(URL+"site-inventory-center/siteinventory/list");
         json = JSONObject.parseObject(response.asString());
        //System.out.println(json);
        Object qtyonhand= JSONPath.eval(json,"data.rows[0].qtyonhand");
        Object qtyobdalloc=JSONPath.eval(json,"data.rows[0].qtyobdalloc");
        Object qtyfrozen=JSONPath.eval(json,"data.rows[0].qtyfrozen");
        Object qtyintransitdoc=JSONPath.eval(json,"data.rows[0].qtyintransitdoc");
        Object qtyintransit=JSONPath.eval(json,"data.rows[0].qtyintransit");


        inventory.setQtyfrozen(Double.valueOf(qtyfrozen.toString()));
        inventory.setQtyintransit(Double.valueOf(qtyintransit.toString()) );
        inventory.setQtyintransitdoc(Double.valueOf(qtyintransitdoc.toString()) );
        inventory.setQtyobdalloc(Double.valueOf(qtyobdalloc.toString()) );
        inventory.setQtyonhand(Double.valueOf(qtyonhand.toString()) );
        inventory.setSitecode(sitecode);
        inventory.setSkucode(skucode);

        return inventory;
    }
    //获取今日0点0分0秒时间戳
    public static long get0Time(){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date zero1 = calendar.getTime();
        return zero1.getTime();
    }
    //获取今日23点59分59秒时间戳
    public static long get24Time(){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        Date zero1 = calendar.getTime();
        return zero1.getTime();
    }

   //查询波次
    public static Response waveList(String wvno){
        response=given()//查询刚生成的波次
                .cookies(cookies)
                .headers("x-yh-wms-whid",VHW078)
                .contentType("application/json")
                .body("{\"pagesize\":50,\"pageindex\":1,\"waveno\":\""+wvno+"\"}")
                .post(URL+"wms-obd-center/wave/list");



        return response;
    }

    public static void waitS(int second) {
        try {
            Thread.sleep(second*1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
